﻿#ifndef IP_LOCATOR_H
#define IP_LOCATOR_H

#include <Windows.h>
#include <stdio.h>
#include <string.h>

#define STR_BUFF_SIZE 128

//IP查询结果结构体
typedef struct _IpLocation
{
	unsigned int IpStart;
	unsigned int IpEnd;
	char * Country;
	char * City;
} IpLocation;

class CIPLocator
{
public:
	inline CIPLocator()
	{
		m_pFile = 0;
	}

	inline ~CIPLocator()
	{
		if (m_pFile != NULL)
		{
			fclose(m_pFile);
		}
	}

public:
	//加载IP数据库
	bool LoadLib(const char *lpPath);
	//IP字符串 -> IP数值
	unsigned int IpStringToInt(const char * ipStr);
	//IP数值 -> IP字符串
	char * IntToIpString(int ipv);
	//查询IP段和所在地
	IpLocation GetIpLocation(unsigned int ipv);

private:
	int getIndexOffset(int fo, int lo, unsigned int ipv);
	char * getString(char * strBuf);

private:
	FILE *m_pFile;
};

#endif